package com.whfc.ms.api.fse.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fse.dto.FseBridgeDTO;
import com.whfc.fse.dto.FseBridgeDataDTO;
import com.whfc.fse.dto.FseBridgeDataLogDTO;
import com.whfc.fse.param.FseBridgeAddParam;
import com.whfc.fse.param.FseBridgeBindParam;
import com.whfc.fse.param.FseBridgeEditParam;
import com.whfc.fse.param.FseBridgeUnbindParam;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.fse.service.MsFseBridgeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Description 架桥机
 * @Author hw
 * @Date 2021-06-01 17:38
 * @Version 1.0
 */
@RequiresPermissions("fse/bridge")
@RestController
@RequestMapping("/ms/api/bridge")
public class FseBridgeController extends BaseController {

    @Autowired
    private MsFseBridgeService msFseBridgeService;

    @GetMapping(value = "/list", params = {"pageNum", "pageSize"})
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "keyword", required = false) String keyword,
                       @RequestParam(value = "bindFlag", required = false) Integer bindFlag,
                       @RequestParam(value = "netState", required = false) Integer netState,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize) {
        logger.info("架桥机列表(分页),loginer:{},pageNum:{},pageSize:{},deptId:{},keyword:{},bindFlag:{},netState:{}",
                user.getUsername(), pageNum, pageSize, deptId, keyword, bindFlag, netState);
        PageData<FseBridgeDTO> pageData = msFseBridgeService.list(deptId, keyword, bindFlag, netState, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }

    @GetMapping(value = "/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "keyword", required = false) String keyword,
                       @RequestParam(value = "bindFlag", required = false) Integer bindFlag,
                       @RequestParam(value = "netState", required = false) Integer netState) {
        logger.info("架桥机列表(分页),loginer:{},deptId:{},keyword:{},bindFlag:{},netState:{}",
                user.getUsername(), deptId, keyword, bindFlag, netState);
        ListData<FseBridgeDTO> pageData = msFseBridgeService.list(deptId, keyword, bindFlag, netState);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/details")
    public Result details(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam("bridgeId") Integer bridgeId) {
        logger.info("乔架机详情,loginer:{},bridgeId:{}", user.getUsername(), bridgeId);
        FseBridgeDTO data = msFseBridgeService.details(bridgeId);
        return ResultUtil.success(data);
    }

    @RequiresPermissions("fse/bridge/device/add")
    @PostMapping("add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody @Validated FseBridgeAddParam param) {
        logger.info("添加架桥机,loginer:{},param:{}", user.getUsername(), param);
        msFseBridgeService.add(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("fse/bridge/device/edit")
    @PostMapping("edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody @Validated FseBridgeEditParam param) {
        logger.info("修改架桥机,loginer:{},param:{}", user.getUsername(), param);
        msFseBridgeService.edit(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("fse/bridge/device/del")
    @PostMapping("/del/{bridgeId}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("bridgeId") Integer bridgeId) {
        logger.info("删除架桥机,loginer:{},bridgeId:{}", user.getUsername(), bridgeId);
        msFseBridgeService.del(bridgeId);
        return ResultUtil.success();
    }

    @PostMapping("/bind")
    public Result bind(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody @Validated FseBridgeBindParam param) {
        logger.info("架桥机绑定硬件,loginer:{},param:{}", user.getUsername(), param);
        param.setUserId(user.getId());
        param.setUserName(user.getNickname());
        msFseBridgeService.bind(param);
        return ResultUtil.success();
    }

    @PostMapping("/unbind/{bridgeId}")
    public Result unbind(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("bridgeId") Integer bridgeId) {
        logger.info("架桥机解绑硬件,loginer:{},bridgeId:{}", user.getUsername(), bridgeId);
        FseBridgeUnbindParam param = new FseBridgeUnbindParam();
        param.setUserId(user.getId());
        param.setUserName(user.getNickname());
        param.setBridgeId(bridgeId);
        msFseBridgeService.unbind(param);
        return ResultUtil.success();
    }


    @GetMapping(value = "/monitor")
    public Result monitor(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestParam("bridgeId") Integer bridgeId) {
        logger.info("架桥机监测,loginer:{},bridgeId:{}",
                user.getUsername(), bridgeId);
        FseBridgeDataDTO data = msFseBridgeService.monitor(bridgeId);
        return ResultUtil.success(data);
    }

    @GetMapping(value = "/bridgeLog")
    public Result bridgeLog(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @RequestParam("bridgeId") Integer bridgeId,
                            @RequestParam("time") @DateTimeFormat(pattern = "yyyy-MM-dd") Date time) {
        logger.info("龙门吊历史记录,loginer:{},bridgeId:{},time:{}",
                user.getUsername(), bridgeId, time);
        ListData<FseBridgeDataLogDTO> list = msFseBridgeService.bridgeLog(bridgeId, time);
        return ResultUtil.success(list);
    }
}
